package Demo1.dao;

import Demo1.bean.Student;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Test1 {
    @Test
    public void selectCondition() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        Student stu = new Student();
        stu.setId(1);
       // stu.setName("诺诺");
       // stu.setAge(22);

        List<Student> list = mapper.selectCondition(stu);

        for (Student student : list) {
            System.out.println(student);
        }
        sqlSession.close();
        is.close();
    }

    @Test
    public void selectByIds() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        Student student = mapper.selectById(1);

        System.out.println(student);

/*        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(2);

        List<Student> list = mapper.selectByIds(ids);

        for (Student student : list) {
            System.out.println(student);
        }*/

        sqlSession.close();
        is.close();
    }

    @Test
    public void selectAll() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

       // PageHelper.startPage(1,2);
        PageHelper.startPage(2,2);
       // PageHelper.startPage(3,2);


        List<Student> list = mapper.selectAll();

        for (Student student : list) {
            System.out.println(student);
        }
        //分页相关参数
        PageInfo<Student>info=new PageInfo<>(list);
        System.out.println("总条数"+info.getTotal());
        System.out.println("总页数"+info.getPages());
        System.out.println("当前页"+info.getPageNum());
        System.out.println("每页显示条数"+info.getPageSize());
        System.out.println("上一页"+info.getPrePage());
        System.out.println("下一页"+info.getNextPage());
        System.out.println("是第一页吗"+info.isIsFirstPage());
        System.out.println("是最后一页吗"+info.isIsLastPage());

        sqlSession.close();
        is.close();
    }
}
